<template>
	<view class="edit_ed">
		<view class="logo_pic">
			<view class="tit_text">请完成以下认证</view>
		</view>
		<view class="input_ediit" v-if="changePwd.type==2">
			<text>请输入{{store.getters.setProcessingTel}}的原密码</text>
			<uni-easyinput placeholderStyle="font-size:28rpx;" v-model="changePwd.oldPassword" type='password'
				placeholder="请输入原密码" @input="nextTick(()=>changePwd.oldPassword=pattern($event))"></uni-easyinput>
			<text>新密码</text>
			<uni-easyinput placeholderStyle="font-size:28rpx;"
				@input="nextTick(()=>changePwd.newPassword=pattern($event))" :clearable="false" type='password'
				v-model="changePwd.newPassword" placeholder="请输入新密码"></uni-easyinput>
		</view>
		<view class="input_ediit_code" v-else>
			<text>请输入{{store.getters.setProcessingTel}}收到的短信验证码</text>
			<view class="input_code">
				<uni-easyinput v-model="changePwd.code" placeholderStyle="font-size:28rpx;" :clearable="false"
					maxlength="6" type="number" placeholder="请输入短信验证码"></uni-easyinput>
				<view class="code_text" v-if="count==0" @click="getCode">
					获取验证码
				</view>
				<view class="code_text_" v-else>
					({{count}}s)后重新获取
				</view>
			</view>
			<text>新密码</text>
			<uni-easyinput :clearable="false" @input="nextTick(()=>changePwd.newPassword=pattern($event))"
				placeholderStyle="font-size:28rpx;" v-model="changePwd.newPassword" type='password'
				placeholder="请输入新密码"></uni-easyinput>
		</view>
		<view class="subtitle" @click="changeType(1)" v-if="changePwd.type==2">
			验证码修改>>
		</view>
		<view class="subtitle" @click="changeType(2)" v-else>
			无法获取验证码？使用密码修改>>
		</view>
		<view class="btn_edit">
			<button type="primary" class="onlogout" :class="{btn_bg:checkPwd(1)}" :disabled="checkPwd(2)"
				@click="changeNameImg">确认修改</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import {
		ref,
		reactive,
		nextTick
	} from "vue"
	import apiCode from '@/util/api/getcode.js'
	import apiUser from '@/util/api/user.js'
	import store from '@/store/index.js'
	import md5 from 'js-md5';
	const changePwd = reactive({
		"code": "", //验证码
		"newPassword": "", //新密码
		"oldPassword": "", //旧密码
		"phone": store.state.userInfo.phone, //手机号
		"type": 2 //更改密码类型：1-验证码改密，2-输入旧密码更改密码
	})
	const count = ref(0) //倒计时
	const clear = ref(null)
	const changeType = (val) => {
		changePwd.type = val
		changePwd.code = ''
		changePwd.newPassword = ''
		changePwd.oldPassword = ''
	}
	// 校验密码非中文
	const pattern = (v) => {
		return v.replace(/[\u4E00-\u9FA5\s]/g, '')
	}
	// 判断是否输入完整
	const checkPwd = (val) => {
		switch (val) {
			case 1:
				return changePwd.type == 2 ? (changePwd.newPassword && changePwd.oldPassword) : (changePwd.code
					.length == 6 && changePwd.newPassword)
			case 2:
				return changePwd.type == 2 ? !(changePwd.newPassword && changePwd.oldPassword) : !(changePwd.code
					.length == 6 && changePwd.newPassword)
			default:
				break;
		}
	}
	// 获取验证码
	const getCode = async () => {
		if (!/^1[^1^2]\d{9}$/.test(changePwd.phone)) {
			uni.showToast({
				title: '请输入正确的手机号!',
				position: 'top',
				icon: "none"
			});
			return;
		}
		count.value = 59
		clear.value = setInterval(() => {
			count.value -= 1
			if (count.value == 0) {
				clearInterval(clear.value)
			}
		}, 1000)
		await apiCode.updateCode({
			phone: changePwd.phone
		}).then(res => {
			if (res.code === 20000) {
				uni.showToast({
					title: res.message,
					icon: 'none',
					position: 'top',
				})
			}
		})
	}
	// 确认修改
	const changeNameImg = async () => {
		//显示加载框
		uni.showLoading({
			title: '加载中',
			mask: true
		});
		let data = JSON.parse(JSON.stringify(changePwd))
		data.newPassword = (md5 as any)(changePwd.newPassword)
		if (changePwd.type == 2) {
			data.oldPassword = (md5 as any)(changePwd.oldPassword)
		}
		await apiUser.updatePwd(data).then(res => {
			if (res.code == 20000) {
				uni.showToast({
					mask: true,
					title: res.message,
					icon: "success"
				})
				setTimeout(()=>{
					uni.reLaunch({
						url: "/pages/login/login?custCode=" + uni.getStorageSync('custCode')
					});
					uni.removeStorageSync('dd-access-user')
				},800)
			}
		})
		//隐藏加载框
		uni.hideLoading();
	}
</script>

<style lang="scss">
	.edit_ed {
		.logo_pic {
			padding: 40rpx 60rpx;

			.tit_text {
				font-weight: bold;
			}
		}

		.input_ediit {
			margin: 0 60rpx;
			font-size: 28rpx;

			:deep(.is-input-border) {
				border-radius: 10rpx !important;
				background-color: rgba(243, 244, 248, 1) !important;
				padding: 10rpx;
				margin: 20rpx 0;
				border: 0rpx solid #999;
				height: 90rpx;

				&.is-focused {

					.uniui-clear,
					.uniui-eye-filled {
						color: #007cff99 !important;
					}
				}

			}
		}

		.input_ediit_code {
			margin: 0 60rpx;
			font-size: 28rpx;

			:deep(.is-input-border) {
				border-radius: 10rpx !important;
				background-color: rgba(243, 244, 248, 1) !important;
				padding: 10rpx;
				margin: 20rpx 0;
				border: none;
				height: 90rpx;
				border: 0rpx solid #999;

				&.is-focused {

					.uniui-clear,
					.uniui-eye-filled {
						color: #007cff99 !important;
					}
				}
			}

			.input_code {
				position: relative;

				.code_text {
					height: 30rpx;
					font-size: 28rpx;
					color: #007cff;
					position: absolute;
					top: calc(50% - 15rpx);
					right: 30rpx;
					z-index: 2;
				}

				.code_text_ {
					height: 30rpx;
					font-size: 28rpx;
					color: #999;
					position: absolute;
					top: calc(50% - 15rpx);
					right: 30rpx;
				}
			}

		}

		.subtitle {
			color: #007cff;
			font-size: 28rpx;
			text-align: right;
			margin: 0 60rpx;
		}

		.btn_edit {
			position: absolute;
			margin: 60rpx auto;
			width: 100%;

			:deep(button:after) {
				border: none !important;
			}

			.onlogout {
				font-size: 28rpx;
				font-weight: bold;
				padding: 0rpx 20rpx;
				margin: 0 36rpx;
				height: 80rpx;
				border-radius: 50rpx;
				line-height: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #007cff;
			}

			.btn_bg {
				background-color: #007cff;
			}
		}
	}
</style>